const app = getApp()
const WXAPI = require('../../wxapi/main')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    partUserInfo: null,
    inviteCode: null,
    userList: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showShareMenu({
      withShareTicket: true
    })
    let that = this
    var _partUserInfo = wx.getStorageSync('partUserInfo')
    if (!_partUserInfo) {
      app.goLoginPageTimeOut()
      return
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    var skey = wx.getStorageSync('loginFlag')
    if (skey) {
      WXAPI.loadCompleteUserInfo({skey})
      .then(resUser => {
        console.log('current user ', resUser)
        if (resUser.code === 0 ) {
          let userAllInfo = resUser.data
          that.setData({
            inviteCode: userAllInfo.myInviteCode
          })
        }
      })
      WXAPI.listInviteUser({skey})
      .then(resUser => {
        console.log('list link users ', resUser)
        if (resUser.code === 0 ) {
          let _userList = resUser.data      
          that.setData({
            userList: _userList
          })
        }else{
          // 没有数据
        }
      })
    }
      
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    console.log('on share')
    let that = this
    if (!that.data.inviteCode) {
       console.log('ssss')
       wx.showToast({
         title: '无效code',
         icon: 'none'
       })
     } else {
      let pathUrl =  '/pages/index/index?inviteCode=' + that.data.inviteCode
      console.log('share path ', pathUrl)
      return {
        title: '打粮人',
        path: pathUrl, // 相对的路径
        imageUrl: '/images/logo.jpg',
        success: (res) => {    // 成功后要做的事情
          var shareTickets = res.shareTickets;
          if (shareTickets.length == 0) {
            return false;
          }
          // console.log(res.shareTickets[0])
          wx.getShareInfo({
            shareTicket: res.shareTickets[0],
            success: (res) => {
              console.log('success', res)
            },
            fail: function (res) { console.log('fail', res) },
            complete: function (res) { console.log('complete ', res) }
          })
        },
        fail: function (res) {
          // 分享失败
          console.log(res)
        }
      }
     }
      // let that = this
    // var _partUserInfo = wx.getStorageSync('partUserInfo')
    // if (!_partUserInfo) {
    //   app.goLoginPageTimeOut()
    //   return
    // } else {
    //   that.setData({
    //     partUserInfo: _partUserInfo
    //   })
    // }   
    // if (res.form == 'button') {
    //   console.log(res.target, res)
    // }
    
  }
})